home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / js.zip / RAIN.JS < prev    next >
Text File  |  1992-12-30  |  4KB  |  148 lines

  1. NB.  This file can be used as a script input file to J Version 6.1
  2. NB.  December 1992
  3.  
  4. NB.  Donald B. McIntyre
  5. NB.  Luachmhor, 1 Church Road
  6. NB.  KINFAUNS, PERTH PH2 7LD
  7. NB.  SCOTLAND - U.K.
  8. NB.  Telephone:  In the UK:      0738-86-726
  9. NB.  From USA and Canada:   011-1-738-86-726
  10.  
  11. NB.   Using J with External Data:  Example of rain data
  12. NB.   VECTOR 1992, In Press
  13. NB.   ASCII file "data.in" is read and file "data.out" is written
  14.    read=. 1!:1 @ <              NB.  Read input file
  15.    write=. [ 1!:2 <@]           NB.  Write to a file.   Fork
  16.    write=. 1!:2 <@]             NB.  Write to a file.   Hook
  17.  
  18. NB.  "data.in" is as ASCII file with 12 rows of 10 columns
  19. NB.  X stands for missing data.   Replace by _1
  20.    y=. read 'data.in'
  21.  
  22. NB. Line feed;  New line;  End of file
  23.    lf=. 10 { a. [ nl=. 13 { a. [ eof=. 26 { a.
  24.    ]i=. u # i. # u=. y = 'X'   NB.  Indices of X in the string
  25.    ,i +/0 1
  26.    (+:#i)$'_1'
  27.    z=. ((+:#i)$'_1') (,i +/0 1)}y   NB. Amend the string
  28.  
  29. NB. Define verbs to amend the string
  30.    h=. 'X'&=@] # i.@#
  31.    h y
  32.    g=. ,@(+/&0 1)@h
  33.    g y
  34.    f=. $&'_1' @ (+:@#@h)
  35.    f y
  36.  
  37.    z-: (f y) (g y)} y
  38.    amend=. '(f y.) (g y.)} y.' : ''
  39.    z-: amend y
  40.  
  41.    amend=. f g@]} ]
  42.    amend
  43.    z-: amend y
  44.    
  45. NB.  Convert character string to numeric matrix
  46.    d=. 12 8 $ ". z #~ -. z e. lf,nl,eof
  47.  
  48. NB.  Illustration of "cut"
  49.    s=. '1 2 3',lf,'4 5 6',lf,'7 8 9',lf
  50.    cut=. <;._2
  51.    cut s
  52.    ;  cut s        NB.  Raze
  53.    ,. cut s        NB.  Ravel
  54.    >  cut s        NB.  Open
  55.    $ ".&.> cut s
  56.    execute=. > @ (".&.> @ <;._2)
  57.    1 + execute s
  58.  
  59.    d -: data=. execute z#~ -. z e. nl,eof
  60.    data
  61.  
  62. NB.  Indexes of missing values represented by _1
  63.    (,u)#i.#,u=. 0>data
  64.  
  65.    h=. 0&>@,
  66.    g=. (] # i.@#)@h     NB.  Fork
  67.    g=. (# i.@#)@ h      NB.  Hook
  68.    ir=. g f.
  69. NB. Index in Ravel of matrix
  70. NB. Fix ir so that the names g and h can be reused
  71.    ir data
  72.  
  73. NB. Row indexes
  74.    <.8%~ir data
  75.    (<.@(%&8)) ir data
  76.    
  77. NB. Column indexes
  78.    8|ir data
  79.  
  80.    {:$ data
  81.  
  82.    row=. <. @ (ir % {:@$)
  83.    row data
  84.    
  85.    column=. {:@$ | ir
  86.    column data
  87.  
  88.    f=. row ,. column
  89.    f data
  90.  
  91.    ix=. <"1 @(row ,. column)  NB.  Row & Column indexes
  92.    ]i=. ix data
  93.    ]z=. i{data
  94.    z-: ({~ ix) data     NB.  Hook
  95.    z-: (ir data){,data
  96.    z-: (ir { ,) data    NB.  Fork
  97.  
  98. NB.  Mean of rows with missing data (represented by _1)
  99.    mean=. +/%#
  100.    ]m=. mean"1 (row data){data
  101.     m-: mean"1 ({~row) data          NB.  Hook
  102.  
  103.    mean n=. 1 _9 2 3 _1 4 0 5
  104.    (>:&0 # ]) n          NB.  Fork
  105.  
  106. NB. Change this fork into a hook
  107.    h=. >:&0              NB.  Monadic
  108.    h n
  109.    g=. #~                NB.  Dyadic
  110.    n g (h n)
  111.    (g h) n               NB.  Hook
  112.    (#~ >:&0) n           NB.  Hook
  113.  
  114. NB.  Mean over positive or zero values
  115.    pzmean=. mean @ (>:&0 # ])
  116.    pzmean n
  117.  
  118. NB.  Replace _1 by row means
  119.    ]m=. pzmean"1 ({~row) data
  120.       ]i=. ir data
  121.       (ir data){,data
  122.       (ir { ,) data
  123.       ]x=. m i}data
  124.       amend=. ir@]} 
  125.       x-: m amend data
  126. NB.  Catenate Annual and Monthly means and the grand mean
  127.    ymean=. ,"2 1 mean     NB.  Hook
  128.    ymean x
  129.    (ymean x),"1 0 (mean"1 x),mean ,x
  130.    
  131.    ]s=. 7.2":((,"2 1 mean) x),"1 0 (mean"1 x),mean ,x
  132.    ]s=. 6.2":((,"2 1 mean) x),"1 0 (mean"1 x),mean ,x
  133.    
  134. NB.  Write an ASCII file with New Line and Line Feed characters
  135.    (,s,"1 nl,lf) write 'data.out'
  136.    
  137. NB.  Display as a table
  138.    f=. <@(7.2&":)
  139.    g=. f&:(,:@ mean"1) ,: f@ mean @ ,
  140.    h=. f , f@ mean
  141.    table =. h ,"0 1 g
  142.    ]t=. table x
  143.    $t
  144.  
  145. NB.  Change the format
  146.    f=. <@(6.2&":)
  147.    ]t=. table x
  148.